package cn.pomelo.secondskill.service.impl;

import cn.pomelo.secondskill.exception.SecondsKillException;
import cn.pomelo.secondskill.mapper.OrdersMapper;
import cn.pomelo.secondskill.model.Orders;
import cn.pomelo.secondskill.service.GoodsService;
import cn.pomelo.secondskill.service.OrdersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    public int deleteByPrimaryKey(String id) {
        return ordersMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Orders record) {
        return ordersMapper.insert(record);
    }

    @Override
    public int insertSelective(Orders record) {
        return ordersMapper.insertSelective(record);
    }

    @Override
    public Orders selectByPrimaryKey(String id) {
        return ordersMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Orders record) {
        return ordersMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Orders record) {
        return ordersMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Orders> findAll() {
        return ordersMapper.selectAll();
    }

    @Override
    public Orders findByUseridAndGoodsId(Integer userId, Integer goodsId, Integer state) {
        return ordersMapper.findByUseridAndGoodsId(userId, goodsId, state);
    }

    @Override
    public List<Orders> findByUserId(Integer userid) {
        return ordersMapper.selectByUserId(userid);
    }

    @Override
    public List<Orders> findByCondition(Orders record) {
        return ordersMapper.selectByCondition(record);
    }

    @Override
    public Integer addCar(Integer userid, Integer goodsId) throws SecondsKillException {
        Orders record = goodsService.doScKill(goodsId, userid, 3);
        return ordersMapper.insert(record);
    }


}

